package org.yty.travelagencyredis.Dao.impl;

import com.alibaba.fastjson.JSON;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;
import org.yty.travelagencyredis.Dao.ChatGroupDao;
import org.yty.travelagencyredis.model.Entity.ChatGroup;

@Repository
@RequiredArgsConstructor
public class ChatGroupDaoImpl implements ChatGroupDao {
    private final StringRedisTemplate redisTemplate;

    @Override
    public ChatGroup selectById(String id) {
        String chatGroupJson = (String) this.redisTemplate.opsForHash().get(ChatGroup.key, id);
        return JSON.parseObject(chatGroupJson, ChatGroup.class);
    }

    @Override
    public boolean insert(ChatGroup chatGroup) {
        this.redisTemplate.opsForHash().put(ChatGroup.key, chatGroup.getId(), JSON.toJSONString(chatGroup));
        return true;
    }

    @Override
    public boolean updateById(ChatGroup chatGroup) {
        this.redisTemplate.opsForHash().put(ChatGroup.key, chatGroup.getId(), JSON.toJSONString(chatGroup));
        return true;
    }

    @Override
    public void deleteById(String id) {
        this.redisTemplate.opsForHash().delete(ChatGroup.key, id);
    }
}
